import { apiOrderDetail, apiOrderClose, apiOrderTakeDelivery } from '../../api/index'
import { formatTime } from '../../common/js/timer'
import { getRoute } from '../../common/js/mixin'
let page = {
  /**
   * 页面的初始数据
   */
  data: {
    orderDetail: {},
    orderId: ''
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad (options) {
    this.setData({
      isIphoneX: getApp().globalData.isIphoneX,
      orderId: options.id,
      helpType: options.help_type || 0
    })
    this.getOrderDetail(options.id)
  },
  timestampToTime (s) {
    if (s > 0) {
      let times = setTimeout(() => {
        var h = Math.floor(s / 3600 % 24)
        var min = Math.floor(s / 60 % 60)
        var sec = Math.floor(s % 60)
        h = this.add(h)
        min = this.add(min)
        sec = this.add(sec)
        if (h > 0) {
          this.setData({
            showTime: `${h}小时${min}分${sec}秒`
          })
        } else {
          this.setData({
            showTime: `${min}分${sec}秒`
          })
        }
        s--
        if (s <= 0) {
          clearTimeout(times)
          this.closeOrder(this.data.orderId)
        } else {
          this.timestampToTime(s)
        }
      }, 1000)
    } else {
      this.closeOrder(this.data.orderId)
    }
  },
  add (m) {
    return m < 10 ? '0' + m : m
  },
  // 获取订单详情
  getOrderDetail (id) {
    apiOrderDetail({ order_id: id }).then(
      res => {
        // 创建时间
        let timer = res.data.order.create_time
        if ((res.data.order.close_time - 0) > 0 && res.data.order.status_name === '待付款') {
          // 结束时间
          let endTime = res.data.order.close_time * 60 + timer
          // 当前时间
          let sec = endTime - new Date().getTime() / 1000
          this.timestampToTime(sec)
        }
        let address = res.data.order.address.replace(/[ ]|[&nbsp;]/g, '')
        for (let i = 0; i < res.data.order.length; i++) {
          res.data.order[i].pay_money = (res.data.order[i].pay_money - 0).toFixed(2)
          res.data.order[i].order_money = (res.data.order[i].order_money - 0).toFixed(2)
        }
        this.setData({
          orderDetail: res.data.order,
          timer: formatTime(timer, 'Y-M-D h:m:s'),
          address
        })
      }
    )
  },
  // 取消订单
  closeOrder (e) {
    apiOrderClose({ order_id: e }).then(
      res => {
        wx.showToast({
          title: '已取消',
          success: () => {
            this.setData({
              orderDetail: {}
            })
            const router = this.getRoute()
            router.getAllOrder()
            this.getOrderDetail(e)
          }
        })
      }
    )
  },
  // 跳转详情
  goToDetail (e) {
    let id = e.currentTarget.dataset.id
    if (this.data.orderDetail.help_user_id > 0) {
      wx.navigateTo({
        url: `../goods-detail/goods-detail?id=${id}&goods_type=1`
      })
    } else {
      wx.navigateTo({
        url: `../goods-detail/goods-detail?id=${id}`
      })
    }
  },
  // 分享赚钱
  shareOrder (e) {
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `../share/share?order_id=${id}`
    })
  },
  // 确认收货
  isSureReceipt (e) {
    console.log(e)
    wx.showModal({
      title: '提示',
      content: '是否确认收货？',
      success: (res) => {
        if (res.confirm) {
          apiOrderTakeDelivery({ order_id: e.target.dataset.id }).then(
            res => {
              console.log(res)
              wx.showToast({
                title: '已收货',
                success: () => {
                  this.getOrderDetail(e.target.dataset.id)
                }
              })
            }
          )
        }
      }
    })
  },
  // 去付款
  toPay (e) {
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `../payment/payment?id=${id}`
    })
  },
  // 查看物流
  lookLogistics (e) {
    let id = e.target.dataset.id
    wx.navigateTo({
      url: `../logistics/logistics?id=${id}`
    })
  },
  // 返回首页
  goHome () {
    wx.switchTab({
      url: `../shopping/shopping`
    })
  },
  // 客服
  serve () {
    wx.showToast({
      title: '暂无客服',
      icon: 'none'
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow () {

  },
  // 监听页面卸载
  onUnload () {
    const router = this.getRoute()
    if (router.getAllOrder) {
      router.setData({
        allOrder: []
      })
      router.getAllOrder()
    }
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage () {

  }
}
getRoute(page)
Page(page)
